package com.jinlejia.erp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/3 0003.
 */

public class RoleAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private ArrayList<Map<String,Object>> data;
    private Map<String,String> typeMap = new HashMap<>();
    private Context context;

    public RoleAdapter(Context context,ArrayList<Map<String,Object>> d) {
        this.mInflater = LayoutInflater.from(context);
        this.data = d;
        this.context = context;
        typeMap.put("1","地址修正人");
        typeMap.put("2","电话添加人");
        typeMap.put("3","限时维护人");
        typeMap.put("4","钥匙责任人");
        typeMap.put("5","图片添加人");
        typeMap.put("6","房源维护人");
        typeMap.put("7","经服维护人");
        typeMap.put("8","金钥匙维护人");
    }

    public void setData(ArrayList<Map<String,Object>> d){
        this.data = d;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        //观察convertView随ListView滚动情况
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_role_info_layout, null);
            holder = new ViewHolder();
            /**得到各个控件的对象*/
            holder.name = convertView.findViewById(R.id.name);
            holder.store = convertView.findViewById(R.id.store);
            holder.role = convertView.findViewById(R.id.role);
            holder.head = convertView.findViewById(R.id.head_img);
            convertView.setTag(holder);//绑定ViewHolder对象
        } else {
            holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
        }
        /**设置TextView显示的内容，即我们存放在动态数组中的数据*/
        Map<String,Object> map = data.get(position);
        holder.name.setText(map.get("responsibleName").toString());
        holder.store.setText(map.get("responsibleDept").toString());
        holder.role.setText(typeMap.get(map.get("type")));

        Glide.with(context).load(ApiConfig.MAGEHEADER_URL().concat(map.get("imageUrl").toString())).placeholder(R.mipmap.ic_role_head).error(R.mipmap.ic_role_head)
                .into(holder.head);

        return convertView;
    }


    /**
     * 存放控件
     */
    public final class ViewHolder {
        public TextView name;
        public TextView role;
        public TextView store;
        public ImageView head;
    }
}
